package com.cskaoyan.service;

import com.cskaoyan.bean.entity.Role;
import com.cskaoyan.bean.entity.RoleExample;
import com.cskaoyan.bean.entity.StorageExample;
import com.cskaoyan.bean.entity.Storage;
import com.cskaoyan.bean.vo.*;
import com.cskaoyan.common.util.BeansCopyUtils;
import com.cskaoyan.common.util.StringUtils;
import com.cskaoyan.mapper.StorageMapper;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * @Author: GaoJia
 * @Date: 2021/10/18 21:10
 */
@Service
public class StorageServiceImpl implements StorageService {

    final StorageMapper storageMapper;

    /**
     * <h3> 从配置文件 获取 文件保存的路径</h3>
     */
    @Value(value = "${file.path}")
    private String fileSavePath;

    public StorageServiceImpl(StorageMapper storageMapper) {
        this.storageMapper = storageMapper;
    }


    /**
     * 根据 key name 以及 param 参数条件查询 对象存储
     */
    @Override
    public BasePageData query(String key, String name, BaseParam param) {

        return getBasePageData(key, name, param);
    }


    /**
     * <h2>  添加 对象存储 </h2>
     * "id"为 null 的问题已经解决
     */
    @Override
    public StorageCommonVo create(MultipartFile file) throws IOException {

        //获得文件名
        String originalFilename = file.getOriginalFilename();
        String key = UUID.randomUUID() + originalFilename;
        //文件类型
        String contentType = file.getContentType();
        //文件大小
        long size = file.getSize();
        String img_url = "http://127.0.0.1:8083/wx/storage/fetch/" + key;
        Date date = new Date();
        Storage storage = new Storage();
        storage.setKey(key);
        storage.setName(originalFilename);
        storage.setType(contentType);
        storage.setSize(((int) size));
        storage.setUrl(img_url);
        storage.setAddTime(date);
        storage.setUpdateTime(date);
        // 判空
        if (StringUtils.isEmpty(originalFilename)) return null;
        // 获得影响行数 调用 insert语句进行插入
        int i = storageMapper.insertSelective(storage);
        if (i == 0) return null;
        //保存到 本地磁盘
        File targetFile = new File(fileSavePath, key);
        file.transferTo(targetFile);

        // 查询ID
        StorageExample storageExample = new StorageExample();
        storageExample.setOrderByClause("add_time desc");
        List<Storage> storageList = storageMapper.selectByExample(storageExample);
        return BeansCopyUtils.copy(storageList.get(0), StorageCommonVo.class);
    }

    /**
     * <h2> 根据主键修改 对象存储 </h2>
     */
    @Override
    public StorageUpdateVo update(Storage storage) {
        storage.setUpdateTime(new Date());
        int i = storageMapper.updateByPrimaryKey(storage);
        if (i == 0) return null;
        Storage storage1 = storageMapper.selectByPrimaryKey(storage.getId());
        return BeansCopyUtils.copy(storage1, StorageUpdateVo.class);
    }

    /**
     * <h2> 根据主键删除 对象存储 </h2>
     */
    @Override
    public int delete(Integer id) {
        return storageMapper.deleteByPrimaryKey(id);
    }

    private BasePageData getBasePageData(String key, String name, BaseParam param) {
        StorageExample storageExample = new StorageExample();
        ;
        StorageExample.Criteria criteria = storageExample.createCriteria();
        if (StringUtils.isNotEmpty(key)) {
            criteria.andKeyLike("%" + key + "%");
        }
        if (StringUtils.isNotEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        //设置排序
        storageExample.setOrderByClause(param.getSort() + " " + param.getOrder());
        PageHelper.startPage(param.getPage(), param.getLimit());
        List<Storage> storages = storageMapper.selectByExample(storageExample);

        return BasePageData.ok(storages);
    }

}
